package com.xulinhao.web;/**
 * @author xulinhao
 * @create 2021-06-02 16:17
 */

import com.xulinhao.pojo.Cart;
import com.xulinhao.pojo.Order;
import com.xulinhao.pojo.OrderItem;
import com.xulinhao.pojo.User;
import com.xulinhao.service.OrderService;
import com.xulinhao.service.impl.OrderServiceImpl;
import com.xulinhao.utils.JdbcUtils;
import com.xulinhao.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author xlh
 * @packagename: com.xulinhao.web
 * @classname: OrderServlet
 * @Description:
 * @Date 2021/6/2
 */
public class OrderServlet extends BaseServlet {

    private OrderService orderService = new OrderServiceImpl();

    /**
     * 生成订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 先获取 Cart 购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        // 获取 Userid
        User loginUser = (User) req.getSession().getAttribute("user");
        if (loginUser == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            return;
        }
        Integer userId = loginUser.getId();
        // 调用 orderService.createOrder(Cart,Userid);生成订单
        String orderId = orderService.createOrder(cart, userId);
        // req.setAttribute("orderId", orderId);
        // 请求转发到/pages/cart/checkout.jsp
        // req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req, resp);
        req.getSession().setAttribute("orderId", orderId);
        req.getSession().setAttribute("userId", userId);
        resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp");
    }

    protected void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过OrderService查询所有订单
        List<Order> orders = orderService.showAllOrders();
        //把全部订单保存到Request域中
        req.setAttribute("orders", orders);
        //重定向到/pages/manager/order_manager.jsp页面
//        resp.sendRedirect(req.getContextPath()+"/pages/manager/order_manager.jsp");
        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req, resp);

    }

    protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("order_id");
        orderService.sendOrder(orderId);
        //重定向到/pages/manager/order_manager.jsp页面
        resp.sendRedirect(req.getContextPath() + "/orderServlet?action=showAllOrders");
    }

    protected void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("order_id");
        String admin = req.getParameter("admin");
        //通过OrderService查询所有订单
        List<OrderItem> orderItems = orderService.showOrderDetail(orderId);
        //把全部订单保存到Request域中
        req.setAttribute("orders", orderItems);
        //重定向到/pages/manager/order_manager.jsp页面
//        resp.sendRedirect(req.getContextPath()+"/pages/manager/order_manager.jsp");
        req.getRequestDispatcher("/pages/order/manager_order.jsp").forward(req, resp);
    }


    //不用重定向才会在页面上输出数据
    protected void showMyOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取 Userid
//        Integer userId = WebUtils.parseInt(req.getParameter("user_id"),0);
        User loginUser = (User) req.getSession().getAttribute("user");
        //通过OrderService查询所有订单
        List<Order> myOrders = orderService.showMyOrders(loginUser.getId());
        //把全部订单保存到域中
        req.setAttribute("myOrders", myOrders);
        //重定向到/pages/manager/order_manager.jsp页面
        req.getRequestDispatcher("/pages/order/order.jsp").forward(req,resp);
//        resp.sendRedirect(req.getContextPath()+"/pages/order/order.jsp");
    }

    protected void receiverOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("order_id");
        orderService.receiverOrder(orderId);
        //重定向到/pages/manager/order_manager.jsp页面
        resp.sendRedirect(req.getContextPath()+"/orderServlet?action=showMyOrders");
    }

}
